//
//  TDRealNameCertificationViewController.m
//  TFB
//
//  Created by Nothing on 15/3/23.
//  Copyright (c) 2015年 TD. All rights reserved.
//

#import "TDRealNameCertificationViewController.h"

#define HANDHELD 100
#define FRONT    101
#define BACK     102

@interface TDRealNameCertificationViewController ()
{
    NSInteger _takeImgTag;
    
    NSString *_sendHandheldImage;
    NSString *_sendFrontImage;
    NSString *_sendBackImage;
}
@end

@implementation TDRealNameCertificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    self.title = @"实名认证";
    
    
    
    [self addTap];
}

- (void)addTap
{
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getPhoto:)];
    self.cardHandheldImageView.tag = HANDHELD;
    self.cardHandheldImageView.userInteractionEnabled = YES;
    [self.cardHandheldImageView addGestureRecognizer:tap1];
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getPhoto:)];
    self.cardFrontImageView.tag = FRONT;
    self.cardFrontImageView.userInteractionEnabled = YES;
    [self.cardFrontImageView addGestureRecognizer:tap2];
    
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getPhoto:)];
    self.cardBackImageView.tag = BACK;
    self.cardBackImageView.userInteractionEnabled = YES;
    [self.cardBackImageView addGestureRecognizer:tap3];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getPhoto:(UITapGestureRecognizer *)sender
{
    _takeImgTag = [[sender view] tag];
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType =UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }
    else{
        [self.view makeToast:@"该设备没有摄像头" duration:2.0f position:@"center"];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSLog(@"照片信息--%@", info);
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self dismissViewControllerAnimated:YES completion:nil];
    
    CGSize imagesize = image.size;
    imagesize.height = 968;
    imagesize.width = 1296;
    NSData *imageData = UIImageJPEGRepresentation([self imageWithImage:image scaledToSize:imagesize], 0.1);
    
    switch (_takeImgTag) {
        case HANDHELD:{
            [self.cardHandheldImageView setImage:image];
            _sendHandheldImage = [imageData base64EncodedString];
        }
            break;
        case FRONT:{
            [self.cardFrontImageView setImage:image];
            _sendFrontImage = [imageData base64EncodedString];
        }
            break;
        case BACK:{
            [self.cardBackImageView setImage:image];
            _sendBackImage = [imageData base64EncodedString];
        }
            break;
            
        default:
            break;
    }
}

//重新绘制图片的尺寸
-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}


//测试实名认证接口
- (void)test
{
    
//    [TDHttpEngine requestForCertificationWithCardHandheld:_sendHandheldImage cardFront:_sendFrontImage cardBack:_sendBackImage custId:@"15040300000025" custMobile:@"13273901442" complete:^(BOOL succeed, NSString *msg, NSString *cod) {
//        
//    }];
}

- (void)bangdingyinhangkaTest
{
//    [TDHttpEngine requestForUpBankCardWithOperType:@"1" oldCardNo:@"" cardNo:@"1111111111111111" cardFront:@"" cardBack:@"" provinceId:@"" cityId:@"" cnapsCode:@"" complete:^(BOOL succeed, NSString *msg) {
//        NSLog(@"绑定银行卡API测试---%@", msg);
//    }];
    
    
}

- (void)jiebangyinhangkaTest
{
//    [TDHttpEngine requestForUpBankCardWithOperType:@"2" oldCardNo:@"1111111111111111" cardNo:@"2222222222222222" cardFront:@"" cardBack:@"" provinceId:@"" cityId:@"" cnapsCode:@"" complete:^(BOOL succeed, NSString *msg) {
//        NSLog(@"绑定银行卡API测试---%@", msg);
//    }];
}


- (IBAction)commit:(id)sender {
    
    //[self bangdingyinhangkaTest];
    //[self jiebangyinhangkaTest];
}

- (IBAction)bangdingBankTest:(id)sender {

}
@end
